#!/usr/bin/env python
# coding: utf-8

# In[1]:


#Importing PulP solver
from pulp import *


# In[2]:


#Inputing the Demand points
demand_points=['Port of Newcastle','Port of Hedland','Port of Vienna','Port of Mina','Port of Antwerp','Port of Santos','Port of Montreal','Port of Vancouver','Port of Shanghai','Port of Hamburg_Czeck','Port of Alexandria','Port of Marseille','Port of Hamburg_Germany','Port of Budapest','Port of Jawaharlal Nehru','Port of Tanjung Priok','Port of Bandar Abbas','Port of Umm Qasr','Port of Haifa','Port of Gioia Tauro','Port of Nagoya','Port of Aqaba','Port of Busan','Port of Shuwaikh','Port of Beirut','Port of Manzanillo','Port of Rotterdam','Port of Salalah','Port of Gaza','Port of Gdansk','Port of Doha','Port of King Abdul Aziz','Port of Jeddah','Port of Bratislava','Port of Durban','Port of Algeciras','Port of Gothenburg','Port of Latakia','Port of Kaohsiung','Port of Bangkok','Port of Mersin','Port of Jebel Ali','Port of Immingham','Port of New York','Port of Los Angeles','Port of Hodeidah']


# In[3]:


demand_points


# In[4]:


#Inputing the Supply Points
supply_points=['Port of Santos','Port of Montreal','Port of Vancouver','Port of Shanghai','Port of Marseille','Port of Bandar Abbas','Port of Haifa','Port of Aktau','Port of Busan','Penang Port','Port of Oslo','Port of Vladivostok','Port of Novorossiysk','Port of St. Petersburg','Port of Belgrade','Port of Mersin','Port of Odessa','Port of New York','Port of Los Angeles']


# In[5]:


supply_points


# In[6]:


#Inputing the distances
distance = dict(zip(supply_points,[dict(zip(demand_points, [7848,8509,8705,9662,6658,0,6795,10037,1804,7027,7529,6204,7026,8591,9552,10371,9351,9933,7667,6528,11468,8005,13783,9888,7626,7223,6749,8408,6360,7745,9594,9716,8432,8693,4838,5426,7293,7553,12691,11741,7528,9407,6799,6148,8718,8624])),
dict(zip(demand_points, [10815,13478,7098,9594,3334,6796,0,8696,11673,3669,6196,4871,3669,7258,9614,12104,9414,9808,6336,5195,10961,6672,11234,9794,6020,5899,3391,8471,5165,3878,9657,9778,7099,7361,10468,4093,3425,6220,13875,12660,6195,9470,3435,1729,7389,7364])),
dict(zip(demand_points, [6731,7508,13490,15871,11472,8403,8708,0,4956,11819,12322,10996,11819,13383,9428,7279,10381,10848,12461,11320,4321,12797,4517,10828,12441,2814,11541,10461,10365,12534,10603,10668,13108,13486,13733,10218,12082,12345,5419,7026,12320,10440,11591,7051,1319,11270])),
dict(zip(demand_points, [5140,3701,10444,6882,11907,13268,11673,4956,0,12277,8365,10048,12277,10330,5293,2909,6557,7139,8336,9335,1007,7912,535,7094,8385,6843,11999,6270,7660,12993,6814,6922,7341,10432,8418,10346,12539,8550,597,2872,8546,6627,12049,10547,5688,6802])),
dict(zip(demand_points, [11599,9088,2980,5332,2339,6204,4871,10996,10048,2708,1805,0,2708,2868,5224,7714,5024,5603,1946,730,10928,2282,10566,5558,1900,8209,2430,4081,1631,3425,5267,5388,2709,2971,7032,778,2971,1830,9485,8270,1806,5080,2481,4293,9700,3245])),
dict(zip(demand_points, [8220,5710,5419,380,6882,9351,9414,10381,6557,7250,3340,5024,7250,5304,1265,4336,0,581,3310,4310,7435,2886,7074,537,3359,12745,6973,943,2922,7967,355,365,2315,5407,4514,5321,7514,3524,5991,4778,3520,173,7023,8862,11057,1776])),
dict(zip(demand_points, [9885,7374,2306,3621,3804,7667,6336,12461,8336,4173,370,1946,4173,2192,3510,6000,3310,3891,0,1230,9214,568,8852,3847,81,9673,3894,2367,83,4889,3553,3674,995,2293,5318,2242,4436,237,7770,6556,233,3366,3944,5756,11048,1534])),
dict(zip(demand_points, [10624,8403,2286,5279,4517,7550,6355,11592,9385,4788,2089,2850,4788,2172,5208,7474,4996,5488,2201,2205,9971,2557,9685,5471,2204,9275,4520,4147,2301,5173,5239,5301,2878,2274,7082,3174,4987,2204,8804,8003,2174,5094,4624,6351,10465,3331])),
dict(zip(demand_points, [5141,3770,10960,7398,12424,13783,11234,4517,535,12793,8882,10566,12793,10846,5810,3426,7074,7655,8852,9852,612,8428,0,7611,8901,6404,12515,6786,8020,13509,7330,7438,7857,10949,8939,10863,13056,9066,1137,3388,9062,7143,12565,10108,5249,7318])),
dict(zip(demand_points, [5076,2565,7419,3856,8882,10534,9839,7359,3025,9251,5340,7024,9251,7305,2268,950,3532,4113,5310,6310,3903,4886,3542,4069,5359,9149,8973,3245,5210,9970,3788,3896,4315,7407,5695,7321,9517,5525,2461,1246,5520,3601,9024,9024,8035,3777])),
dict(zip(demand_points, [14191,11681,5576,7928,793,11585,3521,12184,12643,520,4401,3073,520,5462,7817,10307,7617,8198,4539,3397,13521,4876,13159,8060,4497,9380,708,6675,3831,615,8154,7981,5303,5563,8801,2297,162,4425,12079,10863,10049,7673,813,3955,10791,5842])),
dict(zip(demand_points, [5520,4348,11544,7981,13008,14355,10879,4140,1118,13376,9465,11148,13376,11430,6393,4009,7657,8239,9435,10435,1031,9011,583,8194,9485,6049,13098,7370,8522,14092,7914,8022,8441,11532,9517,11446,13640,9650,1720,3971,9645,7727,13149,9753,4885,7902])),
dict(zip(demand_points, [11406,8893,1312,5139,4251,8117,6782,12907,9854,4619,1572,2392,4619,1197,5029,7519,4829,5410,1715,1678,10732,2087,10371,5366,1672,10103,4342,3886,1295,5338,5072,5193,2680,2513,6839,2689,4885,1577,9290,8074,1552,4885,4392,6246,11613,3052])),
dict(zip(demand_points, [15051,12540,6435,8787,1652,8252,4385,13036,13502,1377,5259,3933,1377,6321,8677,11166,8476,9058,5398,4258,14380,5735,14018,9013,5355,10225,1568,7533,4545,646,8719,8841,6161,6423,9660,3156,960,5283,12938,11722,5258,8532,1674,4824,11600,6700])),
dict(zip(demand_points, [11681,9170,301,5416,4528,8392,6786,13207,10132,4897,1851,2668,4897,198,5306,7796,5106,5687,1994,1955,11010,2364,10648,5643,1949,10396,4619,4163,1990,5614,5349,5470,2791,301,7114,2967,5159,1854,9567,8352,1829,5162,4669,6481,11887,3330])),
dict(zip(demand_points, [10095,7584,2142,3831,3664,7528,6195,12320,8546,4032,546,1806,4032,2028,3720,6210,3520,4102,233,1091,9424,778,9062,4057,191,9516,3755,2577,316,4751,3763,3885,1205,2130,5530,2102,4299,89,7981,6766,0,3576,3804,5617,11024,1744])),
dict(zip(demand_points, [11223,7812,1003,4957,4069,7935,6601,12727,9674,4437,1390,2211,4437,889,4847,7337,4647,5228,1533,1496,10552,1905,10190,5184,1490,9922,4160,3704,1181,5156,4890,5011,2332,992,6657,2508,4704,1395,9110,7894,1370,4703,4210,6064,11432,2871])),
dict(zip(demand_points, [9689,12899,6839,9168,3827,6148,1729,7051,10547,4195,5618,4293,4195,6728,9036,11525,8862,9417,5756,4617,9835,6104,10108,9392,5715,4237,3918,7930,5196,4316,9392,9222,6521,6830,9902,3515,3864,5642,10972,12081,5617,8918,3968,0,5734,7105])),
dict(zip(demand_points, [6466,7885,12171,14552,10154,8718,7389,1319,5688,10522,11025,9700,10522,12056,10104,7910,11057,11524,11048,10024,4992,11501,5249,11504,11122,1490,10245,11137,9280,11242,11279,11344,11927,12158,12410,8922,10789,11049,6129,7730,11024,11116,10295,5734,0,12465]))]))


# In[7]:


distance


# In[8]:


#Inputing the Magnesium Demand
magnesium_demand=dict(zip(demand_points, [550401.9352,9306066.7310,515929.4380,1503264.0000,2831454.8690,312648.0000,4768010.8970,1033272.0550,34475412.5700,9771470.9560,39312.0000,12798252.5200,15804366.3300,1799112.0000,48541273.9200,7320001.8850,130872.0000,8736.0000,1512.0000,1747016.2760,76643749.2700,1644048.0000,55568819.7200,2725411.9480,24192.0000,2643648.0000,866285.8142,168.0000,0.0000,1286998.6790,2701608.0000,3711916.2440,1306243.7560,4809055.6650,1195320.0000,8330786.3350,879743.1429,9576.0000,4467270.1940,16679414.8100,7214200.2270,211848.0000,7285231.3000,42844923.9900,15077364.5600,1680.0000]))


# In[9]:


magnesium_demand


# In[10]:


#Inputing the Magnesium Supply
magnesium_supply=dict(zip(supply_points, [4204466.2090,1222172.6320,363811.2924,298590444.7000,2010234.6250,1585983.9250,7566106.3450,6113002.8630,4078244.4230,47579.5177,5189339.4020,9195220.1200,4484984.5940,4536578.4990,379084.0981,3324539.5030,41313949.6100,10124868.6300,7057279.1490])) 


# In[11]:


magnesium_supply


# In[12]:


# Set Problem Variables
prob = LpProblem("Transportation", LpMinimize)


# In[13]:


prob


# In[14]:


# Set Problem Variables
routes = [(i,j) for i in supply_points for j in demand_points]


# In[15]:


routes


# In[16]:


# Decision Variables
X = LpVariable.dicts("ShipmentAmount",(supply_points, demand_points),0)


# In[17]:


X


# In[18]:


# Objective Function
prob += sum(X[i][j]*distance[i][j] for (i,j) in routes)


# In[19]:


prob


# In[20]:


# Constraints
for j in demand_points:
    prob += sum(X[i][j] for i in supply_points) >= magnesium_demand[j]
    
prob


# In[21]:


# Constraints
for i in supply_points:
    prob += sum(X[i][j] for j in demand_points) == magnesium_supply[i]
prob


# In[22]:


prob.solve()

print("Status:", LpStatus[prob.status])

for v in prob.variables():
    if v.varValue > 0:
        print(v.name, "=", v.varValue)

print("Total kilogram_miles =", value(prob.objective))


# In[ ]:




